<?php namespace Admin\Controller;
class CateController extends CommonController{
	protected $db;
	public function __construct(){
		$this->db = new \Common\Model\Cate;
		parent::__construct();
	}
	public function index(){
		//总计几条
	
		$olddata = $this->db->orderBy("cate_sort","ASC")->get();

		$olddata = Data::tree($olddata,"cate_name","cate_id","cate_pid");
	
		View::with("olddata",$olddata);
		View::make();
	}
	//添加分类
	public function addCate(){
		//获得所有类型名称
	    $typedata =Db::table("360mall_type")->get();
		View::with("typedata",$typedata);
		if(IS_POST){
			if(!$this->db->store()){
			View::error($this->db->getError());
		   }else{
			View::success("添加成功",U('Cate/index'));
		   }
		}
	View::make();
	}
	//添加子类
	public function addSon(){
		$cate_id =intval($_GET['cate_id']);
        $catedata = $this->db->where("cate_id","$cate_id")->field('cate_name','cate_id',"cate_type_id")->first();
	//获得类型名称
		$typedata = Db::table("360mall_type")->where("type_id",$catedata['cate_type_id'])->first();
		View::with("catedata",$catedata);
		View::with("typedata",$typedata);
		if(IS_POST){
			if(!$this->db->addSon()){
			View::error($this->db->getError());
		   }else{
			View::success("添加子类成功",U('Cate/index'));
		  }
		}
		View::make();
	  }
	//编辑子类
	public function edit(){
		$cate_id =intval($_GET['cate_id']);
		//获得旧数据
		$olddata = $this->db->where("cate_id","$cate_id")
		->join("360mall_type","type_id","=","cate_type_id")
		->first();
		View::with("olddata",$olddata);
		//处理所属分类
	     $catedata = $this->db->getCatedata($cate_id);
          View::with("catedata",$catedata);
		  //获得所有类型数据
		$typedata = Db::table("360mall_type")->get();
		 View::with("typedata",$typedata);
		if(IS_POST){
			if(!$this->db->edit($cate_id)){
				View::error($this->db->getError());
		      }else{
			    View::success("编辑子类成功",U('Cate/index'));
			}
		}
		View::make();
	}
	//删除分类
	public function del(){
		$cate_id =intval($_GET['cate_id']);
		$this->db->del($cate_id);
		 $this->success("删除成功",U("Cate/index"));
	}
}
